<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 云盟洗车页面
// +----------------------------------------------------------------------
namespace app\yunmeng\controller;

use app\yunmeng\service\Jssdk;
use think\Controller;
use think\Db;

class Xiche extends Controller
{
  public function index()
  {
    $jsSdk = new Jssdk();
    $json = $jsSdk->signature();
    $config = json_decode($json,true);
    $xiche = Db::name('ym_xiche')->order('cishu desc')->select();
    $this->assign([
      'config' => $config,
      'xiche' => $xiche,
    ]);
    return view();
  }

  public function paix(){
    $gps = Request()->param();
    if ($gps) {
      $fP1Lat = $gps['lat'];
      $fP1Lon = $gps['lng'];
      $xiche = Db::name('ym_xiche')->order('cishu desc')->select();
      foreach($xiche as $k => $v)
      {
        $gps = explode(",", $v['zuob']);
        $fP2Lat = $gps[0];
        $fP2Lon = $gps[1];
        $juli = $this->distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon);
        $xiche[$k]['sort'] = $juli;
        $xiche[$k]['pic'] = getUrl($v['pic']);
      }
      foreach($xiche as $k => $v)
      {
        $sort[$k] = $v['sort'];
      }
      array_multisort($sort,$xiche);
      return json($xiche);
    } else {
      return $this->error('定位失败');
    }
    
  }


  // gps坐标距离计算
  public function distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){
    $fEARTH_RADIUS = 6378137;
    //角度换算成弧度
    $fRadLon1 = deg2rad($fP1Lon);
    $fRadLon2 = deg2rad($fP2Lon);
    $fRadLat1 = deg2rad($fP1Lat);
    $fRadLat2 = deg2rad($fP2Lat);
    //计算经纬度的差值
    $fD1 = abs($fRadLat1 - $fRadLat2);
    $fD2 = abs($fRadLon1 - $fRadLon2);
    //距离计算
    $fP = pow(sin($fD1/2), 2) +
    cos($fRadLat1) * cos($fRadLat2) * pow(sin($fD2/2), 2);
    $juli = intval($fEARTH_RADIUS * 2 * asin(sqrt($fP)) + 0.5);
    return $juli;
  }

}


?>